package com.distribution.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.distribution.common.exception.ServiceException;
import com.distribution.common.utils.StringUtils;
import com.distribution.framework.config.ServerConfig;
import com.distribution.system.domain.entity.CmccDaBanner;
import com.distribution.system.domain.req.CmccDaBannerQueryReq;
import com.distribution.system.domain.req.CmccDaBannerUpdateReq;
import com.distribution.system.mapper.CmccDaBannerMapper;
import com.distribution.system.service.CmccDaBannerService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: GodLu
 * @create: 2024-08-31 17:49
 * @description: 轮播图服务
 */
@Service
public class CmccDaBannerServiceImpl extends ServiceImpl<CmccDaBannerMapper, CmccDaBanner> implements CmccDaBannerService {
    @Value("${swagger.pathMapping}")
    private String pathMapping;
    @Resource
    ServerConfig serverConfig;

    @Override
    public List<CmccDaBanner> selectByQueryParam(CmccDaBannerQueryReq cmccDaBannerQueryReq) {
        List<CmccDaBanner> list = lambdaQuery().like(!StringUtils.isBlank(cmccDaBannerQueryReq.getName()), CmccDaBanner::getName, cmccDaBannerQueryReq.getName())
                .eq(!StringUtils.isBlank(cmccDaBannerQueryReq.getStatus()), CmccDaBanner::getStatus, cmccDaBannerQueryReq.getStatus()).list();
        list.forEach(f -> {
            f.setImgPath(serverConfig.getUrl() + pathMapping + f.getImgPath());
        });
        return list;
    }

    @Override
    public void addBanner(CmccDaBanner cmccDaBanner) {
        // 排序
        sort(cmccDaBanner);
        // 设置图片路径
        cmccDaBanner.setImgPath(cmccDaBanner.getImgPath() == null ? null : cmccDaBanner.getImgPath().replaceAll(serverConfig.getUrl() + pathMapping, ""));
        // 设置时间
        cmccDaBanner.setCreateTime(LocalDateTime.now());
        cmccDaBanner.setUpdateTime(LocalDateTime.now());
        save(cmccDaBanner);
    }

    @Override
    public void updateBanner(CmccDaBannerUpdateReq cmccDaBannerUpdateReq) {
        // 首先保证要修改的轮播图存在
        CmccDaBanner one = lambdaQuery().eq(CmccDaBanner::getId, cmccDaBannerUpdateReq.getId()).one();
        if (one == null) throw new ServiceException("轮播图不存在");
        // 设置名称
        one.setName(cmccDaBannerUpdateReq.getName());
        // 设置排序
        one.setSort(cmccDaBannerUpdateReq.getSort());
        sort(one);
        // 设置图片路径
        one.setImgPath(cmccDaBannerUpdateReq.getImgPath() == null ? null : cmccDaBannerUpdateReq.getImgPath().replaceAll(serverConfig.getUrl() + pathMapping, ""));
        // 设置链接
        if (StringUtils.isNotBlank(cmccDaBannerUpdateReq.getLink())){
            one.setLink(cmccDaBannerUpdateReq.getLink());
        }
        // 设置状态
        one.setStatus(cmccDaBannerUpdateReq.getStatus());
        // 设置更新时间
        one.setUpdateTime(LocalDateTime.now());
        updateById(one);
    }
    private void sort(CmccDaBanner cmccDaBanner){
        List<CmccDaBanner> list = lambdaQuery().ge(CmccDaBanner::getSort, cmccDaBanner.getSort()).list();
        list.forEach(f -> {
            f.setSort(f.getSort() + 1);
            updateById(f);
        });
    }
}
